﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;

namespace PolePosition.v3.Desktop.Grids
{
    public partial class GridXMissionGPS : GridBase, IGridBase
    {
        public GridXMissionGPS()
            : base(Common.Enumerations.DataTypes.ProjectsXmissionGpsVisit)
        {
            InitializeComponent();
            advTreeGPSVisits.Visible = false;
            this.contextMenuBarGrid.SetContextMenuEx(this.advTreeGPSVisits, this.buttonItemContextMenu);
        }

        private bool m_CombosBound = false;
        private void BindCombos()
        {
            if (Program.LookupTableData != null && !m_CombosBound)
            {
                bindingSourceKv.DataSource = Program.LookupTableData.VendorVisitType;
                comboBoxExKv.DisplayMember = EntityLibrary.VendorVisitTypeEnumerations.Columns.Name.ToString();
                comboBoxExKv.ValueMember = EntityLibrary.VendorVisitTypeEnumerations.Columns.Vendor_Visit_TypeId.ToString();
                comboBoxExKv.DataSource = bindingSourceKv;

                bindingSourcePoleSpecies.DataSource = Program.LookupTableData.XMissionStructuresPoleSpecies;
                comboBoxExPoleSpecies.DisplayMember = EntityLibrary.XMissionStructuresPoleSpeciesEnumerations.Columns.Name.ToString();
                comboBoxExPoleSpecies.ValueMember = EntityLibrary.XMissionStructuresPoleSpeciesEnumerations.Columns.XMissionStructures_PoleSpeciesId.ToString();
                comboBoxExPoleSpecies.DataSource = bindingSourcePoleSpecies;

                bindingSourcePoleClass.DataSource = Program.LookupTableData.XMissionStructuresPoleClasses;
                comboBoxExPoleClass.DisplayMember = EntityLibrary.XMissionStructuresPoleClassesEnumerations.Columns.Name.ToString();
                comboBoxExPoleClass.ValueMember = EntityLibrary.XMissionStructuresPoleClassesEnumerations.Columns.XMissionStructures_PoleClassId.ToString();
                comboBoxExPoleClass.DataSource = bindingSourcePoleClass;

                bindingSourcePoleTreatment.DataSource = Program.LookupTableData.XMissionStructuresPoleTreatments;
                comboBoxExPoleTreatment.DisplayMember = EntityLibrary.XMissionStructuresPoleTreatmentEnumerations.Columns.Name.ToString();
                comboBoxExPoleTreatment.ValueMember = EntityLibrary.XMissionStructuresPoleTreatmentEnumerations.Columns.XMissionStructures_PoleTreatmentId.ToString();
                comboBoxExPoleTreatment.DataSource = bindingSourcePoleTreatment;

                bindingSourcePoleTopType.DataSource = Program.LookupTableData.XMissionStructuresPoleTopTypes;
                comboBoxExPoleTopType.DisplayMember = EntityLibrary.XMissionStructuresPoleTopTypeEnumerations.Columns.Name.ToString();
                comboBoxExPoleTopType.ValueMember = EntityLibrary.XMissionStructuresPoleTopTypeEnumerations.Columns.XMissionStructures_PoleTopTypeId.ToString();
                comboBoxExPoleTopType.DataSource = bindingSourcePoleTopType;

                bindingSourceMap.DataSource = Program.LoginResult.Polygons;
                comboBoxExMap.DisplayMember = EntityLibrary.UtilityPolygonEnumerations.Columns.Name.ToString();
                comboBoxExMap.ValueMember = EntityLibrary.UtilityPolygonEnumerations.Columns.Utility_PolygonId.ToString();
                comboBoxExMap.DataSource = bindingSourceMap;

                m_CombosBound = true;
            }
        }

        #region Asynch Processing (Implements IGridBase)

        public override void DoAsynchLoad(DoWorkEventArgs e)
        {
            advTreeGPSVisits.Invoke(new MethodInvoker( delegate() { advTreeGPSVisits.Enabled = false; }));

            int _TotalRecords;
            e.Result = PolePosition.v3.ServiceProxy.UtilityService.GetXmissionGpsProjectPoleData(base.DataKeyId, base.PageSize, base.PageNumber, base.CurrentSearchType, base.CurrentSearchText, out _TotalRecords);
            base.TotalItems = _TotalRecords;
        }

        public override void AsychLoadComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            base.AsychLoadComplete(sender, e);
            //do whatever needs to be done after asynch work complete (BackgroudWorker_RunWorkerCompleted)
            if (e.Cancelled)
            {

            }

            if (e.Error != null || e.Result == null)
            {
                //todo: create some sort of error dialog and use it here instead of messagebx
                DevComponents.DotNetBar.MessageBoxEx.Show("Error retrieving project data.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            List<EntityLibrary.VendorVisitXMissionStructuresPoleEntity> _data;
            _data = e.Result as List<EntityLibrary.VendorVisitXMissionStructuresPoleEntity>;

            if (_data == null || _data.Count() == 0)
            {
                if (base.CurrentSearchType == Common.Enumerations.GridSearchByTpes.None)
                    DevComponents.DotNetBar.MessageBoxEx.Show("No data found for the selected transmission gps project.", "No Data Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    DevComponents.DotNetBar.MessageBoxEx.Show("No data found for the transmission gps project with the applied search.", "No Data Found", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }

            this.bindingSourceGridDataParent.DataSource = _data;
            this.advTreeGPSVisits.DataSource = bindingSourceGridDataParent;
            this.advTreeGPSVisits.Bind();
            this.advTreeGPSVisits.Visible = true;
            advTreeGPSVisits.Enabled = true;
        }

        public override void DoAsynchSave(DoWorkEventArgs e)
        { 
        
        }

        public override void AsychSaveComplete(object sender, RunWorkerCompletedEventArgs e)
        { 
        
        }

        public override void DoAsynchGetKml(DoWorkEventArgs e)
        {
            advTreeGPSVisits.Invoke(new MethodInvoker(delegate() { advTreeGPSVisits.Enabled = false; }));

            base.Kml = PolePosition.v3.ServiceProxy.UtilityService.GetXmissionGpsProjectStructureKML(
                        base.DataKeyId, base.CurrentSearchType, base.CurrentSearchText);
        }

        public override void AsychGetKmlComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            base.AsychGetKmlComplete(sender, e);
            this.advTreeGPSVisits.Enabled = true;

            if (e.Cancelled)
            {
                // not sure what to do
            }

            if (e.Error != null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Error retrieving map data.",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ShowKmlInMap();
        }

        #endregion

        #region Context Menu / Main Bar (Implements IGridBase)

        public override void DoSearch(Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {

        }

        public override void DoExportToExcel()
        {
            //just call method in base
            base.DoExportToExcel();
        }

        public override void DoEdit()
        {
            EntityLibrary.VendorVisitXMissionStructuresPoleEntity _Visit;
            _Visit = advTreeGPSVisits.GetSelectedEntity() as EntityLibrary.VendorVisitXMissionStructuresPoleEntity;

            Editors.EditXmissionGpsVisit _Editor = new PolePosition.v3.Desktop.Editors.EditXmissionGpsVisit(true, _Visit);
            if (_Editor.ShowDialog() == DialogResult.OK)
            {
                //update the record in the grid

            }
            _Editor.Dispose();
        }

        public override void DoOpen()
        {
            EntityLibrary.VendorVisitXMissionStructuresPoleEntity _Visit;
            _Visit = advTreeGPSVisits.GetSelectedEntity() as EntityLibrary.VendorVisitXMissionStructuresPoleEntity;

            Editors.EditXmissionGpsVisit _Editor = new PolePosition.v3.Desktop.Editors.EditXmissionGpsVisit(false, _Visit);
            _Editor.ShowDialog();
            _Editor.Dispose();
        }

        public override void DoMapForGrid()
        {
            StartAsynchGetKml();
        }

        public override void DoMapForRecord()
        {

        }

        public override bool ContextShowAllowed()
        {
            EntityLibrary.VendorVisitXMissionStructuresPoleEntity _Pole;
            _Pole = advTreeGPSVisits.GetSelectedEntity() as EntityLibrary.VendorVisitXMissionStructuresPoleEntity;

            if (_Pole == null)
                return false;

            base.SelectedRowLatitude = _Pole.Latitude;
            base.SelectedRowLongitude = _Pole.Longitude;

            return true;
        }

        #endregion

        public void ShowData(int projectId, string projectName)
        {
            base.DataKeyId = projectId;
            BindCombos();
            base.DataDescription = projectName;
            advTreeGPSVisits.Visible = false;
            StartAsynchLoad();
        }
    }
}
